package com.dyh.quickdemozwei.hxfz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dyh.quickdemozwei.util.DateUtil;
import com.google.common.collect.Lists;
import org.junit.Test;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class TestSpec {


    @Test
    public void test(){

        JSONArray newSkus = JSON.parseArray(
                "[{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2}]}]\n"
        );

        JSONArray oldSkus = JSON.parseArray(
                "[{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2}]},{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.2\"],\"orderSort\":2}]}]\n"
        );

        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> ns = collectSkuSpecSign(newSkus,2);
        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> os = collectSkuSpecSign(oldSkus,2);

        for (String n : ns.getT1()) {
            if (os.getT1().contains(n)){
                System.out.println("规格重复"+n);
            }
        }

        System.out.println();

    }

    @Test public void testXy(){
        System.out.println(String.valueOf(new BigDecimal(12.39).setScale(2,BigDecimal.ROUND_HALF_UP)));
    }


    @Test
    public void test2(){

        JSONArray newSkus = JSON.parseArray(
                "[{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.2\"],\"orderSort\":2}]},{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"红色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.3\"],\"orderSort\":2}]}]\n"
        );

        JSONArray oldSkus = JSON.parseArray(
            "[{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2}]},{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"红色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2}]}]\n"
        );



        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> ns = collectSkuSpecSign(newSkus,2);

        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> os = collectSkuSpecSign(oldSkus,2);


        JSONArray jas = new JSONArray();
        jas.addAll(newSkus);
        jas.addAll(oldSkus);

        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> as = collectSkuSpecSign(jas,2);


        for (String n : ns.getT1()) {
            if (os.getT1().contains(n)){
                System.out.println("规格重复"+n);
            }
        }


        System.out.println(ns.getT2().getKey() >  os.getT2().getKey() );

        System.out.println(ns.getT2().getValue().size() - os.getT2().getValue().size() >=2 );

    }

    @Test
    public void test4(){

        JSONArray newSkus = JSON.parseArray(
            "[{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2},{\"attrId\":\"长度\",\"attrValues\":[\"1\"],\"orderSort\":3}]}]\n"
        );

        JSONArray oldSkus = JSON.parseArray(
            "    [{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"黄色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2}]},{\"saleAttr\":[{\"attrId\":\"颜色\",\"attrValues\":[\"红色\"],\"orderSort\":1},{\"attrId\":\"重量\",\"attrValues\":[\"1.1\"],\"orderSort\":2}]}]\n"
        );



        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> ns = collectSkuSpecSign(newSkus,2);

        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> os = collectSkuSpecSign(oldSkus,2);



        JSONArray jas = new JSONArray();
        jas.addAll(newSkus);
        jas.addAll(oldSkus);

        Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> as = collectSkuSpecSign(jas,2);

        for (String n : ns.getT1()) {
            if (os.getT1().contains(n)){
                System.out.println("规格重复"+n);
            }
        }



        System.out.println(ns.getT2().getKey() >  os.getT2().getKey() );

    }


    @Test public void test5(){
        JSONObject jo = new JSONObject();
        jo.put("xx",new Date());

        System.out.println(jo.getString("xx"));
    }


    Map<String,Map<String,String>>  collectSkuSpecMap(JSONArray ja){
        Map<String,Map<String,String>> nSkuSpecMap = new HashMap<>();

        for (int i = 0; i < ja.size(); i++) {
            JSONObject skuJo = ja.getJSONObject(i);
            JSONArray saleAttrJa =  skuJo.getJSONArray("saleAttr");
            for (int i1 = 0; i1 < saleAttrJa.size(); i1++) {
                JSONObject saleAttrJo = saleAttrJa.getJSONObject(i1);
                nSkuSpecMap.computeIfAbsent(saleAttrJo.getString("attrId"),k->new HashMap<>())
                        .put(saleAttrJo.getJSONArray("attrValues").getString(0),"1");

            }
        }

        return nSkuSpecMap;
    }


    Tuple2<Set<String>, Map.Entry<Integer, Set<String>>> collectSkuSpecSign(JSONArray skuJa, int size){



        Set<String> signs = new HashSet<>();
        TreeMap<Integer,Set<String>> ablMap = new TreeMap<>();
        for (int i = 0; i < skuJa.size(); i++) {
            JSONObject skuJo = skuJa.getJSONObject(i);
            TreeMap<Integer,String> treeMap = new TreeMap<>();

            JSONArray saleAttrJa =  skuJo.getJSONArray("saleAttr");
            for (int i1 = 0; i1 < saleAttrJa.size(); i1++) {
                JSONObject saleAttrJo = saleAttrJa.getJSONObject(i1);

                String sign = String.format("%s:%s",saleAttrJo.getString("attrId"),
                        saleAttrJo.getJSONArray("attrValues").getString(0));
                treeMap.put(saleAttrJo.getInteger("orderSort"),sign);

                ablMap.computeIfAbsent(saleAttrJo.getInteger("orderSort"),k->new HashSet<>())
                        .add(sign);

            }

            signs.add(
                    String.join(";", treeMap.values())
            );
        }





        TreeMap<Integer,Set<String>> lvResMap = new TreeMap<>();

        Integer fk = ablMap.firstEntry().getKey();
        int lv = 0;
        for (String s : ablMap.firstEntry().getValue()) {

            lvResMap.computeIfAbsent(lv,k->new HashSet<>())
                    .add(s);
//            map.ceilingEntry()
        }


        for (Map.Entry<Integer, Set<String>> entry : ablMap.tailMap(fk+1).entrySet()) {
            lv++;
            for (String s : lvResMap.ceilingEntry(lv - 1).getValue()) {
                for (String s1 : entry.getValue()) {

                    lvResMap.computeIfAbsent(lv,k->new HashSet<>())
                            .add(  String.format("%s,%s",s,s1));
                }
            }

        }


        return Tuples.of(signs,lvResMap.lastEntry());
    }


    public static void main(String[] args) {
        TreeMap<Integer,String> treeMap  = new TreeMap<>();

        treeMap.put(2,"x");
        treeMap.put(1,"xx");
        treeMap.put(3,"xxx");


        for (String value : treeMap.values()) {
            System.out.println(value);
        }
    }


    @Test public void test3(){

        // 初始化规格映射
        TreeMap<Integer, Set<String>> map = new TreeMap<>();
        map.put(1, new HashSet<>(Arrays.asList("颜色:红色", "颜色:黄色")));
        map.put(2, new HashSet<>(Arrays.asList("重量:1.3", "重量:1.2")));
        map.put(3, new HashSet<>(Arrays.asList("长度:1", "长度:2")));

        // 用于存储已经生成过的组合的Set
        Set<String> generatedCombinations = new HashSet<>();

        // 遍历规格映射的每一个规格值集合
        for (Map.Entry<Integer, Set<String>> entry : map.entrySet()) {
            Set<String> specs = entry.getValue();
            for (String spec : specs) {
                // 获取规格名称和值
                String[] parts = spec.split(":");
                String name = parts[0];
                String value = parts[1];

                // 生成排列组合结果
                String combination = name + "：" + value;
                if (!generatedCombinations.contains(combination)) {
                    System.out.println(combination);
                    generatedCombinations.add(combination);
                }
            }
        }

    }


    @Test public void testXX(){
        String s = "{\"receipt_type\":\"OTHER\",\"receipt_rel_ids\":{\"string\":[\"3678838164793128503\"]}}";

        JSONObject jo = JSON.parseObject(s);

        JSONObject elseInfo = new JSONObject();
        Optional.ofNullable(jo.getJSONObject("receipt_rel_ids"))
                .map(e->e.getJSONArray("string"))
                .ifPresent(e->elseInfo.put("receiptRelIds",JSON.toJSONString(e)));

        System.out.println();
    }

    @Test public void testYY(){
        Date date = new Date(1702427750L*1000L);

        System.out.println(DateUtil.date2str(date));
    }

    public final static Map<String, String> erpWS2BMSShippingMap = new HashMap<String, String>(){
        {
            put("", "x");

            //erpWS2BMSShippingMap.put(Warehouse.WAREHOUSE_TYPE_PX + "-EMS", "日本邮政");
            put("-EMS", "递四方");
        }
    };



    @Test public void testZ(){
        JSONObject jo = new JSONObject();
        jo.put("x",0);
        jo.put("y","2023-12-22 17:47:27");

        System.out.println();
    }


    @Test public void testY(){
        Set<Integer> s = Lists.newArrayList(1,1,2)
                .stream().collect(Collectors.toSet());

        System.out.println();
    }

}
